1 from tkinter import *
2 import random
3 from tkinter import messagebox
4
5
6 colours = ['Red','Blue','Green','Pink','White',
7 'Yellow','Orange','Purple','Brown']
8
9 score = 0
10
11
12 time = 30
13
14
15 def startGame(event):
16
17 if time==30:
18
19
20 countdown()
21
22
23 nextcolor()
24
25 def nextcolor():
26
27 global score
28 global time
29
30
31 if time > 0:
32
33
34 colour_entry.focus_set()
35
36 if colour_entry.get().lower() == colours[1].lower():
37
38 score += 1
39
40
41 colour_entry.delete(0, END)
42
43 random.shuffle(colours)
44
45
46 colour.config(fg= str(colours[1]) , text = str(colours[0]))
47
48
49 scoreLabel.config(text = "Score: " + str(score))
50
51 def GameEnd():
52 messagebox.showinfo("System", "Game Over")
53
54 def countdown():
55
56 global time
57
58
59 if time > 0 :
60
61
62 time -= 1
63
64
65 timeLabel.config(text = "Time left: "+ str(time))
66
67
68 timeLabel.after(1000, countdown)
69
70 else:
71 GameEnd()
72 if __name__=='__main__':
73
74 root = Tk()
75
76
77 root.title('Guess the Color Game')
78
79
80 root.geometry('375x200')
81 root.config(bg='black')
82
83 instructions = Label(root, text = 'What color? Enter your Guess!', font = ('Helvetica', 12), bg='black', fg='white')
84 instructions.pack()
85
86
87 scoreLabel = Label(root, text = 'Score :'+str(score), fg='white', bg='black', font=('Helvetica' , 12))
88 scoreLabel.pack()
89
90
91 timeLabel = Label(root, text = 'Time Left : '+str(time), fg='white', bg='black', font=('Helvetica' , 12))
92 timeLabel.pack()
93
94
95 colour = Label(root, font=('Helevetica',20), fg='white', bg='black',)
96 colour.pack()
97
98
99 colour_entry = Entry(root, font=('Arial',16))
100
101
102 colour_entry.focus_set()
103 root.bind('<Return>',startGame)
104
105 colour_entry.pack()
106
107 root.mainloop()